package com.salon.client.tabItems;

import java.util.ArrayList;
import java.util.List;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.salon.client.beans.ClientBean;
import com.salon.client.beans.ParentBean;
import com.salon.client.beans.ParentPagingLoadResult;

public abstract class ParentTabItemPanel<T extends ParentBean> extends ContentPanel {
	protected ListStore<BeanModel> store = null;
	protected BasePagingLoader<ParentPagingLoadResult<T>> loader = null;
	protected PagingToolBar toolbar = new PagingToolBar(35);
	protected Grid<BeanModel> grid;
	
	protected T selectedBean = null;
	
	public ParentTabItemPanel(String title) {
		setBodyBorder(false);
		setHeading(title);
		setButtonAlign(HorizontalAlignment.CENTER);
		setLayout(new FitLayout());
		toolbar.setBorders(true);
		setBottomComponent(toolbar);
		
		initComponents();
	}
	
	protected void initComponents() {
		RpcProxy<ParentPagingLoadResult<T>> proxy = getProxy();
		
		BeanModelReader reader = new BeanModelReader();
		// loader and store
		loader = new BasePagingLoader<ParentPagingLoadResult<T>>(proxy,
				reader);
		store = new ListStore<BeanModel>(loader) {
			protected void onLoadException(LoadEvent le) {
				Info.display("Mail Load Error", "Error message was: {0}",
						le.exception.getMessage());
			}
			@Override
			protected void onLoad(LoadEvent le) {
				// TODO Auto-generated method stub
				super.onLoad(le);
				//isFirstSearch = false;
			}
		};
		toolbar.bind(loader);
		refreshStore();
		
		grid = new Grid<BeanModel>(store, getColomnModel());
		grid.setStyleAttribute("borderTop", "none");
		grid.setBorders(true);
		grid.setLoadMask(true);
		grid.getView().setForceFit(true);

		GridSelectionModel<BeanModel> sel = grid.getSelectionModel();
		//sel.setSelectionMode(Style.SelectionMode.SINGLE);
		sel.addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {
					public void handleEvent(
							final SelectionChangedEvent<BeanModel> selEv) {
						List<T> beans = new ArrayList<T>();
						for (BeanModel bean : selEv.getSelection()) {
							T us = bean.getBean();
							beans.add(us);
						}
						setSelected(beans);
					}
				});
		add(grid);
	}
	
	protected void setSelected(List<T> beans) {
		selectedBean = null;
		if (beans != null && beans.size() > 0) {
			selectedBean = beans.get(0);
		}
		
	}
	
	int row = 10;
	protected void addButton(String title, String icon, SelectionListener<ButtonEvent> listener) {
		if (row == 10) {
			toolbar.insert(new SeparatorMenuItem(), ++row);
		}
		
		Button button = new Button(title);
		button.addSelectionListener(listener);
		if (icon != null) {
			button.setIcon(IconHelper.createStyle(icon));
		}
		toolbar.insert(button, ++row);
	}
	
	public void refreshStore() {
		if (store == null)
			return;
		store.removeAll();

		if (loader == null)
			return;
		loader.load();
	}
	
	abstract RpcProxy<ParentPagingLoadResult<T>> getProxy();
	abstract ColumnModel getColomnModel();
}
